package com.game.backend.service.impl.record;

import com.alibaba.fastjson2.JSONObject;
import com.game.backend.mapper.RecordMapper;
import com.game.backend.mapper.UserMapper;
import com.game.backend.model.Record;
import com.game.backend.model.User;
import com.game.backend.service.record.GetRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GetRecordServiceImpl implements GetRecordService {
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public JSONObject getList(Integer page) {
        Integer size = 10;
        Integer st = (page-1)*size;
        List<Record> records = recordMapper.getByPage(st,size);
        JSONObject resp = new JSONObject();
        List<JSONObject> items = new ArrayList<>();
        for(Record record:records){
            JSONObject item = new JSONObject();
            User playerA = userMapper.getById(record.getAId());
            User playerB = userMapper.getById(record.getBId());
            item.put("a_photo",playerA.getPhoto());
            item.put("a_username",playerA.getUsername());
            item.put("b_photo",playerB.getPhoto());
            item.put("b_username",playerB.getUsername());
            String result = "平局";
            if(record.getLoser().equals("A")){
                result = "B赢";
            }else if(record.getLoser().equals("B")){
                result = "A赢";
            }
            item.put("result",result);
            item.put("record",record);
            items.add(item);
        }
        resp.put("records",items);
        resp.put("records_count",recordMapper.getCount());
        return resp;
    }
}
